package com.qdsg.ysg.doctor.ui.activity.prescription;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.qdsg.ysg.doctor.R;
import com.qdsg.ysg.doctor.base.BaseActivity;
import com.qdsg.ysg.doctor.base.BaseApplication;
import com.qdsg.ysg.doctor.ui.adapter.MedicineAdapter;
import com.qdsg.ysg.doctor.ui.adapter.SecondAdapter;
import com.qdsg.ysg.doctor.util.DpUtil;
import com.qdsg.ysg.doctor.util.ImageLoaderHelper;
import com.qdsg.ysg.doctor.util.ToastUtil;
import com.rest.business.RestProxy;
import com.rest.response.BaseResponse;
import com.rest.response.PrescriptionResponse;
import com.rest.response.RegistrationDetailResponse;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * 撤销处方页面
 * Created by HP on 2020/3/2.
 */

public class PrescriptionBackActivity extends BaseActivity {

    @BindView(R.id.img_head)
    ImageView img_head;
    @BindView(R.id.tv_doctor_name)
    TextView tv_doctor_name;

    @BindView(R.id.tv_message)
    TextView tv_message;
    @BindView(R.id.tv_patient_name)
    TextView tv_patient_name;
    @BindView(R.id.tv_patient_detail)
    TextView tv_patient_detail;
    @BindView(R.id.tv_advice)
    TextView tv_advice;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tv_notice)
    TextView tv_notice;

    @BindView(R.id.pre)
    TextView pre;
    @BindView(R.id.main)
    TextView main;
    @BindView(R.id.tail)
    TextView tail;

    @BindView(R.id.second_recyclerView)
    RecyclerView second_recyclerView;

    @BindView(R.id.tv_check)
    TextView tv_check;
    @BindView(R.id.btn_confirm)
    TextView btn_confirm;

    SecondAdapter secondAdapter;
    List<PrescriptionResponse.Second> list2 = new ArrayList<>();

    List<PrescriptionResponse.Prescription> list = new ArrayList<>();
    MedicineAdapter adapter;
    String prescriptionId, diagnoseId, doctorName, patientName, sex, pAge;
    int pFlag;

    @Override
    protected void initViews() {
        LinearLayoutManager manager = new LinearLayoutManager( this );
        manager.setOrientation( LinearLayoutManager.VERTICAL );
        recyclerView.setLayoutManager( manager );
        adapter = new MedicineAdapter( this, list );
        recyclerView.setAdapter( adapter );
        recyclerView.addItemDecoration( new RecyclerView.ItemDecoration() {
            @Override
            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                super.onDraw( c, parent, state );
            }

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets( outRect, view, parent, state );
                outRect.bottom = DpUtil.dip2px( PrescriptionBackActivity.this, 1 );
            }
        } );

        LinearLayoutManager manager2 = new LinearLayoutManager( this );
        manager2.setOrientation( LinearLayoutManager.VERTICAL );
        second_recyclerView.setLayoutManager( manager2 );
        secondAdapter = new SecondAdapter( this, list2 );
        second_recyclerView.setAdapter( secondAdapter );
        second_recyclerView.addItemDecoration( new RecyclerView.ItemDecoration() {
            @Override
            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                super.onDraw( c, parent, state );
            }

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets( outRect, view, parent, state );
                outRect.bottom = DpUtil.dip2px( PrescriptionBackActivity.this, 1 );
            }
        } );
    }

    @Override
    protected void initData() {
        prescriptionId = getIntent().getStringExtra( "prescriptionId" );
        diagnoseId = getIntent().getStringExtra( "diagnoseId" );


        getDetail();
        getRegistrationDetail();
    }

    @OnClick(R.id.btn_confirm)
    void btn_confirm() {
        backPrescription( prescriptionId, "1" );
//        if (pFlag == 0 || pFlag == 1) {
//        } else {
//            backPrescription( prescriptionId, "2" );
//        }
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.aty_prescription_back;
    }


    /**
     * 根据处方id查询处方详情
     *
     * @time 2020/8/25 11:37
     */
    private void getDetail() {
        RestProxy.getInstance().getPrescription( prescriptionId, new Observer<PrescriptionResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(PrescriptionResponse prescriptionResponse) {
                list.clear();
                list.addAll( prescriptionResponse.data.westernList );
                adapter.notifyDataSetChanged();
                tv_advice.setText( "医生建议：" + prescriptionResponse.data.diagnoseResult );
                tv_notice.setText( "用药嘱托：" + prescriptionResponse.data.remark );

//                prescriptionId = prescriptionResponse.data.prescriptionId;
                pFlag = prescriptionResponse.data.flag;
                list2.clear();
                list2.addAll( prescriptionResponse.data.secondList );
                secondAdapter.notifyDataSetChanged();
                pre.setText( prescriptionResponse.data.preAppend );
                main.setText( prescriptionResponse.data.icd );
                tail.setText( prescriptionResponse.data.tailAppend );
                ImageLoaderHelper.getInstance().GlideImageLoader( PrescriptionBackActivity.this, prescriptionResponse.data.url, img_head, R.mipmap.img_default );
                if (pFlag == 0 || pFlag == 1 || pFlag == 2) {
//                    btn_confirm.setText("作废处方");
                    btn_confirm.setVisibility( View.VISIBLE );
                } else {
                    btn_confirm.setVisibility( View.GONE );
                }


                if (pFlag == 0) {//待审核，可撤销
                    tv_check.setText( "待审核" );
                } else if (pFlag == 1) {
                    tv_check.setText( "审核通过，待确认" );
                } else if (pFlag == 2) {
                    tv_check.setText( "审核驳回" );
                } else if (pFlag == 3) {
                    tv_check.setText( "患者已确认" );
                } else if (pFlag == 4) {
                    tv_check.setText( "已撤销" );
                }
            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.onError( PrescriptionBackActivity.this, e );
            }

            @Override
            public void onComplete() {

            }
        } );
    }

    //患者自述相关
    private void getRegistrationDetail() {
        RestProxy.getInstance().getRegistrationDetail( BaseApplication.currentUserId, diagnoseId, new Observer<RegistrationDetailResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(RegistrationDetailResponse registrationDetailResponse) {

                doctorName = registrationDetailResponse.data.doctorName;
                patientName = registrationDetailResponse.data.patientName;
                sex = registrationDetailResponse.data.patientSex;
                pAge = registrationDetailResponse.data.patientAge;
                int diagType = registrationDetailResponse.data.diagType;
                if (diagType == 1) {
                    tv_message.setText( "图文问诊" );
                } else {
                    tv_message.setText( "云门诊" );
                }
                tv_doctor_name.setText( doctorName );
                tv_patient_name.setText( patientName );
                tv_patient_detail.setText( sex + " " + pAge );
            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.onError( PrescriptionBackActivity.this, e );
            }

            @Override
            public void onComplete() {

            }
        } );
    }

    private void backPrescription(String id, String type) {
        RestProxy.getInstance().backPrescription( id, type, new Observer<BaseResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                setResult( RESULT_OK );
                finish();
            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.onError( PrescriptionBackActivity.this, e );
            }

            @Override
            public void onComplete() {

            }
        } );
    }


}
